package com.lagou.rabbitmqdemo.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class RabbitConfig {

    /**
     * 消息队列
     *
     * @return
     */
    @Bean
    public Queue queue() {
        Map<String, Object> props = new HashMap<>();
        // 队列中的消息生存120s
        props.put("x-message-ttl", 120000);
        // 设置死信交换器
        props.put("x-dead-letter-exchange", "ex.go.dlx");
        // 设置死信交换器的routingKey
        props.put("x-dead-letter-routing-key", "go.dlx");

        return new Queue("q.go", true, false, false, props);
    }

    /**
     * 死信队列
     *
     * @return
     */
    @Bean
    public Queue queueDlx() {
        return new Queue("q.go.dlx", true, false, false);
    }

    /**
     * 消息交换器
     * @return
     */
    @Bean
    public Exchange exchange() {
        return new DirectExchange("ex.go", true, false, null);
    }

    /**
     * 死信交换器
     * @return
     */
    @Bean
    public Exchange exchangeDlx() {
        return new DirectExchange("ex.go.dlx", true, false, null);
    }

    /**
     * 消息交换器绑定消息队列
     * @return
     */
    @Bean
    public Binding binding() {
        return BindingBuilder.bind(queue()).to(exchange()).with("go").noargs();
    }

    /**
     * 死信交换器绑定死信队列
     * @return
     */
    @Bean
    public Binding bindingDlx() {
        return BindingBuilder.bind(queueDlx()).to(exchangeDlx()).with("go.dlx").noargs();
    }
}
